// pages/p_purchasecar_doing/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
      itemlist:[],
      material:[],
      vendor:[],
      userdefine:[],
      quantity:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
//载入时，生成采购车明细------2025-03-07
var that=this;
wx.request({
  url:'http://localhost:8081/basis_api/miniprogram/getcommoninfo',
  method:'POST',
  data:{
    x_id:1,
    source:'purchase_car',
    tdata:null,
  },
  header:{'content-type':'application/json'},
  success(res){
    var tdata=res.data.data[0];
    var temp_userdefine:any[]=[];
    var temp_quantity:any[]=[];
    var temp2:any[]=[];
    // 通过循环取数，进行初始化
       for(let i=0;i<tdata.length;i++){
          //  商品列表信息
           temp2.push({ 
                       id:tdata[i].id,
                       mid:tdata[i].material_id,
                       code:tdata[i].code,
                       name:tdata[i].name,
                       invstd:tdata[i].invstd,
                       invstd2:tdata[i].invstd2,
                       munit_name:tdata[i].munit_name,
                       quantity:tdata[i].quantity,
                       userdefine:JSON.parse(tdata[i].userdefine),
                       vendor:JSON.parse(tdata[i].vendor),
                       v:tdata[i].v,
                       addition:tdata[i].addition=='null'?[]:JSON.parse(tdata[i].addition),
                      //  userdefine_doc:p2[i].doc==null?[]:JSON.parse(p2[i].doc),
                     });
                    // 商品数量信息
                    temp_quantity.push({id:tdata[i].id,
                                        mid:tdata[i].material_id,
                                        quantity:tdata[i].quantity});
                    // 商品自定义项信息
                    var temp_userdefine2=JSON.parse(tdata[i].userdefine);
                    temp_userdefine2.map((uitem:any)=>{
                        temp_userdefine.push({
                             id:tdata[i].id,
                             mid:tdata[i].material_id,
                             uid:uitem.userdefine_id,
                             v:uitem.v,
                             input_style:uitem.input_style,
                           });
                        });
                    };
        if(temp2.length!=0)
                that.setData({
                     itemlist:temp2,
                     quantity:temp_quantity,
                     userdefine:temp_userdefine,
                  });
            console.log('载入的采购车明细');
            console.log(tdata);
     },
    });
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 保存选择的物料ID
   checkMaterial: function (e:any){
       var mid=e.detail.value;
       var id=e.target.dataset.id;
       var material=this.data.material;
       console.log(mid);
       if(mid.length==0)
          material=material.filter((mitem:any)=>mitem.id!=id);
       else 
          material.push({id,mid:parseInt(mid[0])});
       this.setData({
          material,
      });
      console.log('选择的物料ID');
       console.log(material);
   },
  //  保存选择的供应商信息
   checkVendor:function (e:any){
     var id=e.target.dataset.id;
     var mid=e.target.dataset.mid;
     var v=e.detail.value;
     var vendor=this.data.vendor;
     vendor=vendor.filter((item:any)=>item.id!=id);
    vendor.push({
       id,
       mid,
       vid:v,
    });
    this.setData({
      vendor,
    });
    console.log('选择的供应商');
    console.log(vendor);
   },
  //  记录数量变化
   setQuantity:function(e:any){
     var mid=e.target.dataset.mid;
     var id=e.target.dataset.id;
     var v=e.detail.value;
     var temp_quantity=this.data.quantity;
     temp_quantity=temp_quantity.filter((item:any)=>item.id!=id);
     temp_quantity.push({
           id,
           mid,
           quantity:v,
     });
     this.setData({
        quantity:temp_quantity,
     });
     console.log('选择的数量');
     console.log(this.data.quantity);
   },
  //  保存定义项的变更
  setUserdefine:function(e:any){
    var mid=e.target.dataset.mid;
    var id=e.target.dataset.id;
    var uid=e.target.dataset.uid;
    var input_style=e.target.dataset.input_style;
    var v=e.detail.value;
     
    var temp_userdefine=this.data.userdefine;
    temp_userdefine=temp_userdefine.filter((item:any)=>!(item.id==id&&item.uid==uid));
    temp_userdefine.push({
         mid,id,uid,input_style,v,
    });
    this.setData({
      userdefine:temp_userdefine,
    });
    console.log(temp_userdefine);
  },
  // 删除选中的商品2025-3-10----------------------------------------------------------------------
  del2car:function (e:any){
    //  先汇总选中的物料编码，再远程调用
    var that=this;
    var ids=that.data.material.map((item:any)=>item.id);
    console.log('待删除的行ID');
    console.log(ids);
    wx.request({
      url:'http://localhost:8081/basis_api/miniprogram/updatecommoninfo',
      method:'POST',
      data:{
        x_id:1,
        operater_id:1,
        type:1,//1为新增，0为修改，3为其他
        source:'del_purchase_car',
        tdata:{ids},
      },
      header:{'content-type':'application/json'},
      success(res){
        wx.showToast({
          title:'删除成功',
        });
        //从列表中隐藏删除的项
        var temp_itemlist=that.data.itemlist.filter((item:any)=>!(ids.includes(item.id)));
        that.setData({
          itemlist:temp_itemlist,
        });
        console.log('删除后的商品列表');
        console.log(temp_itemlist);
      },
    });
  }


})